package com.itaem.community.modules.announcement.controller;



import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.announcement.entity.AnnouncementEntity;
import com.itaem.community.modules.announcement.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;


/**
 * <p>
 * 公告 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "公告管理")
@ResponseResult
@RestController
@RequestMapping("/announcement")
@Slf4j
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 分页查询所有公告
     * @param page 第几页
     * @param size 每页少条
     * @return 分页信息
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page<AnnouncementEntity> list(@PathVariable Long page, @PathVariable Long size){
        return announcementService.page(new Page<>(page, size));
    }

    /**
     * 根据id获取单条公告
     * @param id 公告id
     * @return 该id的公告
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("获取单条信息")
    @GetMapping("/{id}")
    public AnnouncementEntity info(@PathVariable Long id){
        return announcementService.getById(id);
    }

    /**
     * 新增公告
     * @param announcementEntity 新的公告
     * @return 新增是否成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("新增")
    @PostMapping
    public Boolean save(@RequestBody AnnouncementEntity announcementEntity) {
        //默认新增不被删
        announcementEntity.setDeleted(false);
        //代码设置发布时间
        announcementEntity.setPublicationTime(LocalDate.now());
        return announcementService.save(announcementEntity);
    }

    /**
     * 根据id修改公告信息
     * @param announcementEntity 修改的公告
     * @return 是否修改成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("更新")
    @PatchMapping
    public Boolean update(@RequestBody AnnouncementEntity announcementEntity) {
        return announcementService.update(
                announcementEntity,
                //条件控制选择对应id的公告
                new UpdateWrapper<AnnouncementEntity>().eq("id",announcementEntity.getId()));
    }

    /**
     * 根据公告id删除某一公告（逻辑删除）
     * @param id 要删除的公告id
     * @return 是否删除成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Boolean remove(@PathVariable Long id) {
        //根据id逻辑删除某一公告（番茄配置好了直接remove就是逻辑删除）
        AnnouncementEntity announcement = announcementService.getById(id);
        if(announcement == null){
            log.info("无此公告或该公告已删除");
            return true;
        }
        return announcementService.removeById(id);
    }

}